from u import Sess, U
from pathlib import Path
import json
async def get_X_from_X0_Y(
        p_X0: Path | str, p_Y: Path | str,
        p_Xo = Path.cwd() / '10a-default-oneshot.X.md', p_Yo = Path.cwd() / '10a-default-oneshot.Y.yml'):

    p_X0 = Path(p_X0) if not isinstance(p_X0, Path) else p_X0
    p_Y = Path(p_Y) if not isinstance(p_Y, Path) else p_Y

    assert p_Xo.exists(), f"X oneshot file {p_Xo} does not exist"
    assert p_Yo.exists(), f"Y oneshot file {p_Yo} does not exist"
    p_t = Path.cwd() / '10a-XY-template.md'
    n = p_Y.name.removesuffix('Y.yml')
    p_X = p_Y.with_name(f"{n}X.md")

    # 2. replace the template with the oneshot and Y
    s = p_t.read_text()
    s1 = s.format(
        X_oneshot=p_Xo.read_text(),
        Y_oneshot=p_Yo.read_text(),
        Y=p_Y.read_text(),
        X0=p_X0.read_text()
    )

    # 3. prepare the session
    p_qa = Path.cwd().parent / "ixc-qa"
    k = json.loads(Path("/home/me/.ssh/k-250611-h3c.json").read_text())
    s = Sess.new(model="google/gemini-2.5-flash-preview-05-20:thinking", url=k['url'], key=k['key'])

    s.add_q(s1); await s.ask_for_a(); None

    o = s.ms[-1].content
    o = U.extract_code_block(o, 'markdown')
    # o = extract_code_block(o, 'markdown')
    print(f'{p_X.write_text(o)} bytes written to {p_X}')


# Example usage:
# await get_X_from_X0_Y(p_X0 = Path.cwd() / 'XY'/ '05-01.X0.md', p_Y = Path.cwd() / 'XY' / '05-01.Y.yml')
